//
//  ST_QuickMessageVoiceViewModel.swift
//  btvideo
//
//  Created by mac on 2021/9/8.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa

class ST_QuickMessageVoiceViewModel {
    
    struct Config {
        let title:String
        let message:String
        let msgDesc:String
    }
    
    enum UploadType {
        /// 快捷搭讪使用
        case quickMessage
        /// 语音签名使用
        case voiceSign
    }
    
    
    let config:Config
    let type:UploadType
    
    init(config:Config,type:UploadType) {
        self.config = config
        self.type = type
    }
    
    
    /// 页面状态
    let state = BehaviorRelay<ST_QuickMessageVoiceViewController.State>(value: .ready)
    
    /// 上传语音文件到云储存
    /// - Parameter url: 语音文件本地路径
    /// - Returns: key
    func uploadVoice(url: URL) -> Single<(URL, String)> {
        
        Single.create { single in
            
            guard let data = try? Data(contentsOf: url) else {
                single(.failure(ST_MyError.description("获取语音数据失败")))
                return Disposables.create()
            }
            
            let key = UUID().uuidString
            
            JVUpload(fileKey: key, data: data, uploadProgress: { p in
                
            }, success: { json in
                single(.success((url, key)))
            }, failure: { (code, error) in
                single(.failure(ST_MyError.description(error)))
            })
            
            return Disposables.create()
        }
        
    }
    
    /// 添加语音快捷搭讪消息
    /// - Parameters:
    ///   - voiceURL: 语音URL
    ///   - duration: 语音时长
    /// - Returns: Single
    func add(voiceURL: URL, key: String) -> Single<Void> {
        guard let duration = try? AVAudioPlayer(contentsOf: voiceURL).duration else {
            return Single.error(ST_MyError.description("获取语音数据失败"))
        }
        
        return Single.create { single in
            
            switch self.type {
            case .voiceSign:
                
                
                JVrequest(PH.modifyUserGeneralInfoApi, parameters: ["voice_sign": key, "duration": duration], success: { json in
                    single(.success(()))
                }, failure: { (code, error) in
                    single(.failure(ST_MyError.description(error)))
                })
                
            case .quickMessage:
                
                JVrequest(PH.addQuickMessageListApi, parameters: ["content": key, "type": QuickMessageCategory.voice.rawValue, "duration": duration], success: { json in
                    single(.success(()))
                }, failure: { (code, error) in
                    single(.failure(ST_MyError.description(error)))
                })
            }
            
            
            return Disposables.create()
        }
    }
}


//MARK: - Generator
extension ST_QuickMessageVoiceViewModel{
    
    static func defaultVMCreator() -> ST_QuickMessageVoiceViewModel {
        return ST_QuickMessageVoiceViewModel(config: Config(title: "录制语音", message: "录制一段自己的声音", msgDesc: "可以是自我介绍、交友意向、唱歌等，最多60秒"), type: .quickMessage)
    }
    
    
    static func recordVoiceSignVMCreator() -> ST_QuickMessageVoiceViewModel {
        
        return ST_QuickMessageVoiceViewModel(config: Config(title: "录制语音签名", message: "录制一段自己的声音", msgDesc: "可以是自我介绍、交友意向、唱歌等，最多60秒"), type: .voiceSign)
        
    }
    
}
